﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DSLFactory.VSCTDesigner
{
    partial class CommandWithIconBase
    {
        public string GetCommandName()
        {
            string name = Strings[0].CommandName;
            if( String.IsNullOrEmpty(name) )
                name = Strings[0].ButtonText;
            if( name.StartsWith("cmdid") )
                name = name.Substring(5);
            return NormalizeName(name);
        }

        public static string NormalizeName(string name)
        {
            if( String.IsNullOrEmpty(name) )
                return "_";

            StringBuilder sb = new StringBuilder();
            if( Char.IsDigit(name[0]) )
            {
                sb.Append("_");
            }

            bool containsLowerChars = false;
            for( int i = 0; i < name.Length; i++ )
            {
                if( char.IsLower(name, i) )
                {
                    containsLowerChars = true;
                    break;
                }
            }

            bool newWord = true;
            char prevChar = ' ';
            int upperCount = 0;
            for( int i = 0; i < name.Length; i++ )
            {
                char ch = name[i];
                if( char.IsLetterOrDigit(ch) )
                {
                    if( !newWord && containsLowerChars )
                    {
                        if( char.IsUpper(ch) && char.IsLower(prevChar) )
                        {
                            upperCount = 0;
                            newWord = true;
                        }
                        else if( char.IsUpper(ch) && char.IsUpper(prevChar) && upperCount < 3 )
                        {
                            upperCount++;
                            newWord = true;
                        }
                        else
                            upperCount = 0;
                    }
                    prevChar = ch;

                    if( newWord )
                    {
                        ch = char.ToUpper(ch);
                    }
                    else
                    {
                        ch = char.ToLower(ch);
                    }

                    newWord = false;
                    sb.Append(ch);
                }
                else
                {
                    newWord = true;
                    if( ch == '_' )
                        sb.Append(ch);
                }
            }
            name = sb.ToString();
            return name;
        }

        /// <summary>
        /// Gets the display name value.
        /// </summary>
        /// <returns></returns>
        internal string GetDisplayNameValue()
        {
            return Strings.Count > 0 ? Strings[0].ButtonText : String.Empty;
        }

        /// <summary>
        /// Sets the display name value.
        /// </summary>
        /// <param name="newValue">The new value.</param>
        internal void SetDisplayNameValue(string newValue)
        {
            if( Strings.Count == 0 )
            {
                StringDef str = new StringDef(this.Store);
                str.ButtonText = newValue;
                Strings.Add(str);
            }
            else
            {
                Strings[0].ButtonText = newValue;
            }
        }
    }
}
